#include <pd_gpio.h>
#include <hd_led.h>
#include <hd_key.h>
#include <hd_oled.h>
#include <delay.h>
#include <hd_oled_protocol_i2c.h>
#include <hd_oled_protocol_spi.h>
#include <pd_i2c_hd.h>
#include <pd_i2c_sim.h>
#include <pd_spi_io.h>
#include <stddef.h>
#include <pd_spi_hd.h>
#include <pd_spi_sim.h>
#include <hd_flash.h>
#include "main.h"

extern OLED *oled;

extern void oled_instance();
extern void SystemClock_Config(void);

int main(void) {
    SystemClock_Config();

    GPIO *ledGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_0);
    Light *led = LEDBuilder.build(ledGPIO);
    led->init(led);

    // PB6/PF4/BOOT0 share one pin
    GPIO *keyGPIO = GPIOBuilder.build(GPIOF, LL_GPIO_PIN_4);
    Key *key = KeyBuilder.build(keyGPIO);
    key->init(key);

    oled_instance();

    oled->full(oled);
    oled->refresh(oled);
    delay_ms(1000);

    oled->clear(oled);
    oled->refresh(oled);
    delay_ms(1000);

    oled->showBinNum(oled, 0, 0, 0xCFE, 12);
    oled->showHexNum(oled, 104, 0, 0xCFE, 3);

    oled->showHexNum(oled, 0, 16, 0xA1B2D3, 6);

    oled->showSignedNum(oled, 0, 32, 123, 3);
    oled->showSignedNum(oled, 40, 32, -123, 3);
    oled->showFloatNum(oled, 80, 32, (float) 1.2, 3);

    oled->showString(oled, 0, 48, "ASCII:");
    oled->printf(oled, 64, 48, "CODE:");
    oled->refresh(oled);

    uint8_t keyNum;
    uint8_t c = ' ';
    while (1) {
        keyNum = key->getKeyNum(key);
        if (keyNum != 1) {
            led->turn(led);

            if (c++ > '~') c = ' ';

            oled->showChar(oled, 48, 48, c);
            oled->showNum(oled, 104, 48, c - ' ', 3);

            oled->mode = OLED_MODE_NEGATIVE;
            oled->size = OLED_SIZE_24;
            oled->showChar(oled, 116, 16, c);

            oled->mode = OLED_MODE_POSITIVE;
            oled->size = OLED_SIZE_12;
            oled->showNum(oled, 80, 16, c - ' ', 3);
            oled->showChar(oled, 104, 16, c);

            oled->size = OLED_SIZE_16;
            oled->refresh(oled);

            delay_ms(1000);
        }
    }

}
